package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;

import com.cskaoyan.bean.MarketCategoryL1;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.service.AdminCategoryService;
import com.cskaoyan.util.TransactionUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminCategoryServiceImpl implements AdminCategoryService {
    @Override
    public CommonData query() {
        TransactionUtil.startTransaction();
        MarketCategoryMapper mapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(0).andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = mapper.selectByExample(marketCategoryExample);


        for (MarketCategory marketCategory : marketCategories) {
        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        marketCategoryExample1.createCriteria().andPidEqualTo(marketCategory.getId()).andDeletedEqualTo(false);
        List<MarketCategory> childerns = mapper.selectByExample(marketCategoryExample1);
        marketCategory.setChildren(childerns);
        }

        TransactionUtil.commit();
        return CommonData.data(marketCategories);
    }

    @Override
    public CommonData queryL1() {
        TransactionUtil.startTransaction();
        MarketCategoryMapper mapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andLevelEqualTo("L1");
        List<MarketCategory> marketCategories = mapper.selectByExample(marketCategoryExample);

        ArrayList<MarketCategoryL1> marketCategoryL1s = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            MarketCategoryL1 marketCategoryL1 = new MarketCategoryL1();
            marketCategoryL1.setValue(marketCategory.getId());
            marketCategoryL1.setLabel(marketCategory.getName());
            marketCategoryL1s.add(marketCategoryL1);
        }

        TransactionUtil.commit();
        return CommonData.data(marketCategoryL1s);
    }

    @Override
    public MarketCategory create(MarketCategory marketCategory) {
        TransactionUtil.startTransaction();
        MarketCategoryMapper mapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        marketCategory.setDeleted(false);
        //mapper.insertSelective()
        mapper.insert(marketCategory);
        TransactionUtil.commit();
        return marketCategory;
    }

    @Override
    public void update(MarketCategory marketCategory) {
        TransactionUtil.startTransaction();
        MarketCategoryMapper marketCategoryMapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        marketCategory.setUpdateTime(new Date());
        marketCategoryMapper.updateByPrimaryKey(marketCategory);
        TransactionUtil.commit();
    }

    @Override
    public void delete(MarketCategory marketCategory) {
        TransactionUtil.startTransaction();
        MarketCategoryMapper marketCategoryMapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        marketCategory.setDeleted(true);
        marketCategory.setUpdateTime(new Date());
        marketCategoryMapper.updateByPrimaryKey(marketCategory);
        TransactionUtil.commit();
    }

}
